package com.party.core.service.circle.biz;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.PartyCode;
import com.party.core.exception.BusinessException;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.circle.*;
import com.party.core.model.commune.IntegralType;
import com.party.core.model.gatherInfo.GatherInfoMember;
import com.party.core.model.member.Member;
import com.party.core.model.member.MemberAct;
import com.party.core.model.member.MemberGroup;
import com.party.core.model.member.PartnerMember;
import com.party.core.model.system.RoleType;
import com.party.core.model.system.SysRole;
import com.party.core.service.circle.ICircleApplyService;
import com.party.core.service.circle.ICircleMemberService;
import com.party.core.service.circle.ICircleMemberTagService;
import com.party.core.service.circle.ICircleService;
import com.party.core.service.commune.ICIntegralBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.member.IPartnerMemberService;
import com.party.core.service.member.biz.MemberPersonalCountBizService;
import com.party.core.service.system.ISysRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 圈子业务逻辑接口 party Created by Juliana on 2016-12-14
 */

@Service
public class CircleMemberBizService {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICircleMemberTagService circleMemberTagService;
    @Autowired
    private ICircleService circleService;
    @Autowired
    private ICircleMemberService circleMemberService;
    @Autowired
    private CircleTopicBizService circleTopicBizService;
    @Autowired
    private ICircleApplyService circleApplyService;
    @Autowired
    private IPartnerMemberService partnerMemberService;
    @Autowired
    private MemberPersonalCountBizService memberPersonalCountBizService;
    @Autowired
    private ICIntegralBizService integralBizService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private ISysRoleService sysRoleService;


    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 查询所有圈子成员关系
     *
     * @param circleMember
     * @param params
     * @param tags         @return
     */
    public List<ListAllMember> listAll(CircleMember circleMember, Map<String, Object> params, String tags) {
        circleMember.setDelFlag(BaseModel.DEL_FLAG_NORMAL);

        Set set = null;
        if (!Strings.isNullOrEmpty(tags)) {
            List<String> tagList = Arrays.asList(tags.split(","));
            set = new HashSet(tagList);
        }

        List<ListAllMember> circleList = circleMemberService.listAll(circleMember, params, set);
        return circleList;
    }

    /**
     * 通过类型查询所有圈子成员关系
     *
     * @param circleMember
     * @param params
     * @param tags         @return
     */
    public List<ListAllMember> listAllByType(CircleMember circleMember, Map<String, Object> params, String tags) {
        circleMember.setDelFlag(BaseModel.DEL_FLAG_NORMAL);

        Set set = null;
        if (!Strings.isNullOrEmpty(tags)) {
            List<String> tagList = Arrays.asList(tags.split(","));
            set = new HashSet(tagList);
        }

        List<ListAllMember> circleList = circleMemberService.listAllByType(circleMember, params, set);
        return circleList;
    }

    /**
     * 移出会员
     *
     * @param circleMember
     */
    @Transactional
    public void out(CircleMember circleMember, String curUserId) {
        Circle circle = circleService.get(circleMember.getCircle());
        if (circle != null) {
            //操作者 curUserId
            //创建者 circle.getCreateBy()
            //被删除者 circleMember.getMember()
            if (circle.getCreateBy().equals(circleMember.getMember())) {
                List<CircleMember> circleMembers = circleMemberService.listPageSearch(new CircleMember(circleMember.getCircle(), null), null, new Page(1, 2));
                if (circleMembers.size() == 2) {
                    throw new BusinessException("该圈子下还有其他成员不能直接删除创建者");
                }
            } else if ((!circle.getCreateBy().equals(circleMember.getMember())) && circleMember.getMember().equals(curUserId)) {
                throw new BusinessException("你不能自己删除自己");
            }
            // 删除用户标签
            CircleMemberTag search = new CircleMemberTag();
            search.setMember(circleMember.getMember());
            search.setCircle(circleMember.getCircle());
            List<CircleMemberTag> tagList = circleMemberTagService.list(search);
            for (CircleMemberTag tag : tagList) {
                circleMemberTagService.delete(tag.getId());
            }
            // 删除circleMember关系
            CircleMember cMember = circleMemberService.getUnique(circleMember);
            if (cMember != null) {
                circleMemberService.delete(cMember.getId());
            }
            // 删除用户申请
            CircleApply applySearch = new CircleApply();
            applySearch.setCircle(cMember.getCircle());
            applySearch.setMember(cMember.getMember());
            circleApplyService.delBySearch(applySearch);
            // 删除圈子话题关系（不删除话题）
            CircleTopic topicSearch = new CircleTopic();
            topicSearch.setCreateBy(cMember.getMember());
            topicSearch.setCircle(circleMember.getCircle());
            circleTopicBizService.delBySearch(topicSearch);
            // 更新圈子会员数
            updateMemberNum(circle.getId());

        }
    }

    /**
     * 根据圈子id删除会员信息
     *
     * @param id 圈子id
     */
    @Transactional
    public void delByCircle(String id) {
        CircleMember search = new CircleMember();
        search.setCircle(id);
        List<CircleMember> list = circleMemberService.list(search);
        for (CircleMember circleMember : list) {
            circleMemberService.delete(circleMember.getId());
        }
    }

    /**
     * 保存圈子成员
     *
     * @param cId       圈子
     * @param ids       用戶
     * @param curUserId 當前用戶
     * @param source    用戶來源
     */
    @Transactional
    public void saveBiz(String cId, String ids, String curUserId, Integer source) {
        if (!Strings.isNullOrEmpty(ids) && !Strings.isNullOrEmpty(cId)) {
            Circle circle = circleService.get(cId);
            String[] idsArray = ids.split(",");
            int count = 0;
            for (String id : idsArray) {
                id = id.trim();
                CircleMember cMember = circleMemberService.getUnique(new CircleMember(cId, id));
                if (cMember == null) {
                    Member member = memberService.get(id);
                    if (member != null) {
                        CircleMember circleMember = new CircleMember();
                        circleMember.setCircle(cId);
                        circleMember.setMember(id);
                        circleMember.setCreateBy(curUserId);
                        circleMember.setUpdateBy(curUserId);
                        circleMember.setIsAdmin(YesNoStatus.NO.getCode());
                        circleMember.setSource(source);
                        circleMemberService.insert(circleMember);
                        ++count;

                        try {

                            PartnerMember mainOrg = partnerMemberService.getMainOrg(member.getId(), circle.getCreateBy());

                            // 机构用户关系
                            PartnerMember partnerMember = new PartnerMember();
                            partnerMember.setPartnerId(circle.getCreateBy());
                            partnerMember.setMemberId(id);
                            partnerMemberService.getSet(partnerMember);
                            // 个人统计表添加数据
                            memberPersonalCountBizService.getSet(circle.getCreateBy(), id);
                            try {
                                //行知公社业务
                                boolean isCommune = false;
                                Map<String, Object> stringObjectMap = Maps.newHashMap();
                                stringObjectMap.put("type", RoleType.PARTNER_ROLE.getCode());
                                stringObjectMap.put("memberId", circle.getCreateBy());
                                List<SysRole> sysRoles = sysRoleService.getRoleByMemberId(stringObjectMap);
                                for (SysRole s : sysRoles) {
                                    if ("knowing_commune".equalsIgnoreCase(s.getCode())) {
                                        isCommune = true;
                                    }
                                }
                                if (isCommune) {
                                    //添加赠送积分
                                    if (mainOrg == null) {
                                        //如果主分社为空，说明是第一次进入分社
                                        //添加赠送积分

                                        MemberGroup partner = memberGroupService.getPartner(circle.getCreateBy());
                                        integralBizService.insertSystemCIntegralItem(null, partner.getId(), partnerMember, null, IntegralType.CINTEGRALTYPE_CATEGORY_GIFT.getCode());
                                    } else {
                                        integralBizService.addGiftIntegral(mainOrg.getPartnerId(), mainOrg.getMemberId());
                                    }

                                }
                            } catch (Exception e) {
                                logger.error("添加赠送积分失败:" + JSONObject.toJSONString(member), e.getMessage());
                            }
                        } catch (Exception e) {
                            logger.error("保存机构关系异常", e);
                        }
                    }
                }
            }

            int memberNum = circle.getMemberNum() == null ? 0 : circle.getMemberNum();
            memberNum += count;
            circle.setMemberNum(memberNum);
            circleService.update(circle);
        } else {
            throw new BusinessException(PartyCode.PARAMETER_ILLEGAL, "cId或ids不能为空");
        }
    }

    /**
     * 设置为管理员
     *
     * @param id
     */
    @Transactional
    public CircleMember setMgrBiz(String id) {
        CircleMember circleMember = circleMemberService.get(id);
        if (circleMember != null) {
            if (circleMember.getIsAdmin() == 1) {
                throw new BusinessException("管理员已存在，请勿重复设置");
            }
            CircleMember search = new CircleMember();
            search.setCircle(circleMember.getCircle());
            search.setIsAdmin(1);
            List<CircleMember> list = circleMemberService.list(search);
            if (null != list && list.size() >= 5) {
                throw new BusinessException("管理员人数不能超过5个");
            }
            circleMember.setIsAdmin(1);
            circleMemberService.update(circleMember);
            return circleMember;
        } else {
            throw new BusinessException(PartyCode.PARAMETER_ILLEGAL, "id错误");
        }
    }

    /**
     * 取消管理员
     *
     * @param id
     */
    @Transactional
    public void cancelMgrBiz(String id) {
        CircleMember circleMember = circleMemberService.get(id);
        if (circleMember != null) {
            circleMember.setIsAdmin(0);
            circleMemberService.update(circleMember);
        } else {
            throw new BusinessException(PartyCode.PARAMETER_ILLEGAL, "id错误");
        }
    }

    /**
     * 更新圈子会员数
     *
     * @param circleId
     */
    public void updateMemberNum(String circleId) {
        List<CircleMember> results = circleMemberService.list(new CircleMember(circleId));
        Circle circle = circleService.get(circleId);
        circle.setMemberNum(results.size());
        circleService.update(circle);
    }

    /**
     * 圈子成員管理
     *
     * @param memberActs
     * @param circleId
     * @param businessMemberIds
     */
    @SuppressWarnings("unchecked")
    public void circleMemberManage(List<MemberAct> memberActs, String circleId, String type, List<String> businessMemberIds, String currentUserId) {

//		Circle circle = circleService.get(circleId);

        // 获取圈子会员
        List<CircleMember> circleMembers = circleMemberService.list(new CircleMember(circleId));
        List<String> circleMemberIds = (List<String>) CollectionUtils.collect(circleMembers, new Transformer() {

            @Override
            public Object transform(Object input) {
                CircleMember mm = (CircleMember) input;
                return mm.getMember();
            }
        });

        // 报名人员
        List<String> actMemberIds = (List<String>) CollectionUtils.collect(memberActs, new Transformer() {

            @Override
            public Object transform(Object input) {
                MemberAct mm = (MemberAct) input;
                return mm.getMemberId();
            }
        });

        Set<String> newActMemberIds = Sets.newHashSet(actMemberIds);

        newActMemberIds.addAll(businessMemberIds);

//		String oldMemberIds = actMemberIds.toString().replace("[", "").replace("]", "");

        // 添加未加入圈子的报名人员
        newActMemberIds.removeAll(circleMemberIds);
        if (newActMemberIds.size() > 0) {
            String memberIds = newActMemberIds.toString().replace("[", "").replace("]", "");
            saveBiz(circleId, memberIds, currentUserId, 1); // 业务生成圈子成员
        }

//		if (type.equals("1")) { // 已成功人员圈子 移除圈子中已取消的报名人员
//			if (circleMemberIds.size() > 0) {
//				List<String> oldMemberIdList = new ArrayList<String>();
//				for (String memberId : oldMemberIds.split(",")) {
//					oldMemberIdList.add(memberId.trim());
//				}
//				circleMemberIds.removeAll(oldMemberIdList);
//				if (circleMemberIds.size() > 0) {
//					for (String memberId : circleMemberIds) {
//						if (!memberId.equals(circle.getCreateBy())) {
//							CircleMember circleMember = new CircleMember(circle.getId(), memberId);
//							out(circleMember,currentUserId);
//						}
//					}
//				}
//			}
//		}

        // 更新圈子创建者为管理员
        updateCreateBy(circleId);

        // 更新会员数
        updateMemberNum(circleId);
    }

    /**
     * 圈子成员管理
     *
     * @param gatherInfoMembers
     * @param circleId
     * @param businessMemberIds
     */
    @SuppressWarnings("unchecked")
    public void circleMemberManageGatherinfo(List<GatherInfoMember> gatherInfoMembers, String circleId, List<String> businessMemberIds, String currentUserId) {
        // 获取圈子会员
        List<CircleMember> circleMembers = circleMemberService.list(new CircleMember(circleId));
        List<String> circleMemberIds = (List<String>) CollectionUtils.collect(circleMembers, new Transformer() {
            @Override
            public Object transform(Object input) {
                CircleMember mm = (CircleMember) input;
                return mm.getMember();
            }
        });
        // 报名人员
        List<String> ginfoMemberIds = (List<String>) CollectionUtils.collect(gatherInfoMembers, new Transformer() {
            @Override
            public Object transform(Object input) {
                GatherInfoMember mm = (GatherInfoMember) input;
                return mm.getMemberId();
            }
        });
        Set<String> newActMemberIds = Sets.newHashSet(ginfoMemberIds);
        newActMemberIds.addAll(businessMemberIds);
        // 添加未加入圈子的报名人员
        newActMemberIds.removeAll(circleMemberIds);
        if (newActMemberIds.size() > 0) {
            String memberIds = newActMemberIds.toString().replace("[", "").replace("]", "");
            saveBiz(circleId, memberIds, currentUserId, 1); // 业务生成圈子成员
        }
        // 更新圈子创建者为管理员
        updateCreateBy(circleId);

        // 更新会员数
        updateMemberNum(circleId);
    }

    /**
     * 更新圈子创建者为管理员
     *
     * @param circleId
     */
    public void updateCreateBy(String circleId) {
        Circle circle = circleService.get(circleId);
        CircleMember searchMember = new CircleMember();
        searchMember.setCircle(circleId);
        searchMember.setMember(circle.getCreateBy());
        CircleMember circleMember = circleMemberService.getUnique(searchMember);
        if (circleMember != null && circleMember.getIsAdmin() != null && circleMember.getIsAdmin().equals(YesNoStatus.NO.getCode())) {
            circleMember.setIsAdmin(YesNoStatus.YES.getCode());
            circleMemberService.update(circleMember);
        }
    }

    /**
     * 圈子列表
     *
     * @param params 查询参数
     * @param page   分页参数
     * @return 列表
     */
    public List<WithTage> withTageList(Map<String, Object> params, Page page) {
        List<WithTage> list = circleMemberService.withTageList(params, page);
        return LangUtils.transform(list, new Function<WithTage, WithTage>() {
            @Override
            public WithTage apply(WithTage withTage) {
                String tags = Joiner.on(",").join(withTage.getCircleTagList());
                withTage.setTags(tags);
                return withTage;
            }
        });
    }
}
